package com.smday.stack;

/**
 * @author Summerday
 */
public class Calculator {

    public static void main(String[] args) {

        String expression = "30+2*6-2";

        //创建两个栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);

        //定义需要的相关变量
        int index = 0;

        int num1;
        int num2;

        int oper;
        int result;
        char ch;

        StringBuilder keepNum = new StringBuilder();
        do {
            //依次得到表达式中的每个字符

            ch = expression.substring(index, index + 1).charAt(0);
            //如果是运算符
            if (operStack.isOper(ch)) {
                //运算符栈不为空
                if (!operStack.isEmpty()) {

                    //符号栈不为空,有操作符,当前操作符的优先级<=栈中的优先级

                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        result = numStack.cal(num1, num2, oper);
                        //运算结果入数栈
                        numStack.push(result);
                        //当前运算符入符号栈
                        operStack.push(ch);
                    } else {

                        //当前操作符的优先级>栈中的优先级
                        operStack.push(ch);
                    }
                } else {
                    //栈为空,直接入栈
                    operStack.push(ch);
                }


            } else {
                keepNum.append(ch);
                //数字直接入栈
                //记录index
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum.toString()));
                } else {
                    //处理多位数,向表达式的index后再看一位,如果是数就进行扫描,如果符号才入栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        //最后一位是运算符
                        numStack.push(Integer.parseInt(keepNum.toString()));
                        keepNum = new StringBuilder();
                    }
                }
            }
            //index+1,判断是否扫描到expression最后
            index++;
        } while (index < expression.length());
        //扫描完毕
        while (!operStack.isEmpty()) {

            //如果符号栈为空,则计算到最后的结果,数栈中只有一个数字
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            result = numStack.cal(num1, num2, oper);
            numStack.push(result);
        }

        System.out.printf("表达式%s = %d", expression, numStack.pop());


    }
}

//用数组实现栈
class ArrayStack2 {

    private int maxSize;
    private int top;
    private int[] stack;

    ArrayStack2(int maxSize) {
        //栈的大小
        this.maxSize = maxSize;
        //数组
        stack = new int[maxSize];
        //栈顶,初始化-1
        top = -1;
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int data) {
        if (isFull()) {
            System.out.println("栈已满!");
            return;
        }
        stack[++top] = data;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空!");
        }
        int value = stack[top--];
        System.out.printf("pop -->  %d\n", value);
        return value;

    }

    //增加方法返回当前栈顶的值,但不出栈
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空!");
        }
        return stack[top];
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈空,没有数据");
            return;
        }
        System.out.println("=======STACK=======");
        System.out.println();
        System.out.printf("top -->  stack[%d]=%d\n", top, stack[top]);
        for (int i = top - 1; i > 0; i--) {
            System.out.printf("         stack[%d]=%d\n", i, stack[i]);
        }
        System.out.printf("bot -->  stack[%d]=%d\n", 0, stack[0]);

    }

    //返回运算符的优先级,程序员确定,返回的数字越大,优先级越高

    public int priority(int oper) {

        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }

    }

    //判断是不是运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //计算方法
    public int cal(int num1, int num2, int oper) {
        //计算结果
        int result = 0;

        switch (oper) {

            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num2 - num1;
                break;
            case '*':
                result = num2 * num1;
                break;
            case '/':
                result = num2 / num1;
                break;
            default:
                break;

        }

        return result;
    }
}

